package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Zhuce extends AppCompatActivity {

    Button btn1;
    EditText user;
    EditText password;
    EditText password2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);

        btn1 = this.findViewById(R.id.zhuce);
        user = this.findViewById(R.id.wrp_user);
        password = this.findViewById(R.id.wrp_password);
        password2 = this.findViewById(R.id.wrp_password2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String Auser = user.getText().toString();
                String Apassword = password.getText().toString();
                String Apassword2 = password2.getText().toString();

                if (Auser.equals("")){
                    Toast.makeText(Zhuce.this,"请输入用户名",Toast.LENGTH_LONG).show();
                }else if (Apassword.equals("")){
                        Toast.makeText(Zhuce.this,"请输入密码",Toast.LENGTH_LONG).show();
                    }else if (Apassword.equals(Apassword2) ){

                        SharedPreferences sp = getSharedPreferences(Auser,MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("username",Auser); //键值对
                        editor.putString("password",Apassword); //键值对
                        editor.commit();



                    File root = Environment.getExternalStorageDirectory();
                    String path = root.getAbsolutePath()+"/xuesuhao";//创建文件名
                    File dir = new File(path);
                    if (!dir.exists()){
                        dir.mkdir();
                    }
                    File file = new File(dir,Auser+".txt");
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                            FileWriter fileWriter = new FileWriter(file);
                            fileWriter.write("账号："+Auser+" "+"密码"+Apassword);
                            fileWriter.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }


                        Intent intent = new Intent(Zhuce.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                        Toast.makeText(Zhuce.this,"注册成功",Toast.LENGTH_LONG).show();
                    }else {
                    Toast.makeText(Zhuce.this,"密码输入不一致",Toast.LENGTH_LONG).show();
                }
                    }


        });
    }


    public void oc_concel(View view) {
        Intent intent = new Intent(Zhuce.this,MainActivity.class);
        startActivity(intent);
        finish();
    }
}
